// 处理和全局页面相关的请求

const express = require('express');
const router = express.Router();
const { sqlFn } = require('../mysql');


// 首页
router.get('/menuList', (req, res, next) => {
    const sql = 'SELECT * FROM menu';
    sqlFn(sql, [], (err, results) => {
        if (err) {
            console.log(err);
            next(err);
        }
        function buildTree(list, pid = -1) {
            return list
                .filter(item => Number(item.pid) === Number(pid))
                .map(item => {
                    const children = buildTree(list, item.id);
                    return children.length > 0
                        ? { ...item, children }
                        : { ...item };
                });
        }
        res.json({
            code: 200,
            data: buildTree(results)
        });
    })
});


module.exports = router;